package com.liuning.Thread;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * 联系Thread 多线程同步下载图片
 */
public class TestThread2 extends Thread{

    /**
     * 网络图片地址
     */
    private String url;

    /**
     * 保存的文件名
     */
    private String name;

    public TestThread2(String url,String name) {
        this.url = url;
        this.name = name;
    }

    /**
     * 下载图片线程的执行体
     */
    @Override
    public void run() {
        WebDownloader downloader = new WebDownloader();
        downloader.downloader(url,name);
        System.out.println("下载了文件名为：" + name);
    }

    public static void main(String[] args) {
        //下载到当前目录
        TestThread2 t1 = new TestThread2("http://47.93.247.196:8088/uploadFileServer/getFile?fileName=boy01.png", "1.jpg");
        TestThread2 t2 = new TestThread2("http://47.93.247.196:8088/uploadFileServer/getFile?fileName=boy01.png", "2.jpg");
        TestThread2 t3 = new TestThread2("http://47.93.247.196:8088/uploadFileServer/getFile?fileName=boy01.png", "3.jpg");

        t1.start();
        t2.start();
        t3.start();
    }
}

//下载器
class WebDownloader {
    //下载方法
    public void downloader(String url,String name) {
        try {
            FileUtils.copyURLToFile(new URL(url),new File(name));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("下载异常");
        }
    }
}
